// component/listSearch/listSearch.js
import * as transferData from './../../models/transferData'

Component({
  /**
   * 组件的配置
   */
  options: {
    styleIsolation: 'isolated',
    pureDataPattern: /^_/
  },

  /**
   * 组件的传入样式
   */
  externalClasses: ['style-extra'],

  /**
   * 组件的属性列表
   */
  properties: {
    isCustomHeader: {
      type: String,
      value: '0',
    },
    isHideCount: {
      type: String,
      value: '0',
    },
    isHideCity: {
      type: String,
      value: '0',
    },
    isHideSearch: {
      type: String,
      value: '0',
    },
    history: {
      type: String,
      value: '',
    },
    accurate: {
      type: String,
      value: '',
    },
    accurateType: {
      type: String,
      value: 'id',
    },
    placeholder: {
      type: String,
      value: '搜索',
    },
    cityName: {
      type: String,
      value: '',
    },
    cityCode: {
      type: String,
      value: '',
    },
    local: {
      type: Boolean,
      value: false,
    },
    static: {
      type: Boolean,
      value: false,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    searchText: '',
  },

  /**
   * 组件的生命周期
   */
  lifetimes: {
    attached: function () {
      let t = this
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /**
   * 页面的生命周期
   */
  pageLifetimes: {
    show: function () {
      let t = this
      t.checkSearchKeyword()
    },
    hide: function () {
      // 页面被隐藏
    },
    resize: function (size) {
      // 页面尺寸变化
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onSearchClose() {
      let t = this
      t.setData({
        searchText: '',
      })
      t.triggerEvent('searchChange', {
        label: '',
        value: '',
        type: ''
      })
    },

    onSearchJump() {
      let t = this
      let {
        history,
        accurate,
        placeholder,
        cityCode,
        cityName,
        searchText,
        accurateType,
        isHideCount
      } = t.data
      wx.navigateTo({
        url: `/pages/home/search/search?search=${searchText}&history=${history}&accurate=${accurate}&placeholder=${placeholder}&citycode=${cityCode}&cityname=${cityName}&accuratetype=${accurateType}&count=${isHideCount}`,
      })
    },

    checkSearchKeyword() {
      let t = this
      let data = transferData.getData('commonSearch')
      transferData.resetData('commonSearch')
      if (data) {
        t.setData({
          searchText: data.label,
        })
        t.triggerEvent('searchChange', data)
      }
    },

    onCityJump() {
      if (this.data.static) {
        this.triggerEvent("clickCity")
        return
      }
      wx.navigateTo({
        url: `/pages/home/switchCity/switchCity?${this.data.local ? 'local=1' : ''}`,
      })
    },

  }
})